package com.cacheserverdeploy.depoly;

import java.util.ArrayList;
import java.util.List;

import com.cacheserverdeploy.algorithm.FirstStep;
import com.cacheserverdeploy.algorithm.PSO;
import com.cacheserverdeploy.algorithm.SecondStep;
import com.cacheserverdeploy.algorithm.ThirdStepCopy;
import com.cacheserverdeploy.node.ConsumeNode;
import com.cacheserverdeploy.node.NetworkNode;
import com.cacheserverdeploy.util.Const;
import com.cacheserverdeploy.util.Util;

public class Deploy {
	/**
	 * 你需要完成的入口 <功能详细描述>
	 * 
	 * @param graphContent
	 *            用例信息文件
	 * @return [参数说明] 输出结果信息
	 * @see [类、类#方法、类#成员]
	 */
	public static String[] deployServer(String[] graphContent) {
		/** do your work here **/

		Util.parseFirstLine(graphContent[0]);
		// 解析第三行
		Util.parseThridLine(graphContent[2]);

		// 创建网络节点数组
		NetworkNode[] networkNodes = Util.initializeNetworkNodes();
		// 解析网络链路
		Util.parseLinks(networkNodes, graphContent, 4, 4 + Const.NETWORK_LINK_NUM);
		// 解析并创建消费节点
		ConsumeNode[] consumeNodes = Util.parseAndInitializeConsumeNode(networkNodes, graphContent,
				5 + Const.NETWORK_LINK_NUM, graphContent.length);

		if(Const.CONSUME_NODE_NUM<100){
			PSO pso = new PSO();
			pso.setNumofStation(networkNodes.length);
			pso.setKmax(10);
			pso.setM(400);//种群个数
			List<Integer> bestSever=pso.PSOAlgorithm(networkNodes, consumeNodes);
			
			List<Integer> bestSever1=new ArrayList<>();
			for (int i = 0; i < bestSever.size(); i++) {
				if (bestSever.get(i)==1) {
					bestSever1.add(i);
				}
			}
			ThirdStepCopy step3=new ThirdStepCopy();
			int mincost=step3.adjust(bestSever1, networkNodes, consumeNodes);
			System.out.println(mincost);
			String[] result=step3.resultOutPut(step3.getSeverPathsResult(), networkNodes,consumeNodes);
			return result;
		}else if (Const.CONSUME_NODE_NUM>100&&Const.CONSUME_NODE_NUM<200) {
			
			PSO pso = new PSO();
			pso.setNumofStation(networkNodes.length);
			pso.setKmax(8);
			pso.setM(100);//种群个数
			List<Integer> bestSever=pso.PSOAlgorithm(networkNodes, consumeNodes);
			
			List<Integer> bestSever1=new ArrayList<>();
			for (int i = 0; i < bestSever.size(); i++) {
				if (bestSever.get(i)==1) {
					bestSever1.add(i);
				}
			}
			ThirdStepCopy step3=new ThirdStepCopy();
			int mincost=step3.adjust(bestSever1, networkNodes, consumeNodes);
			System.out.println(mincost);
			String[] result=step3.resultOutPut(step3.getSeverPathsResult(), networkNodes,consumeNodes);
			return result; 
		}else {
			 //创建邻接矩阵
	         int[][] matrix=Util.constructMatrix(networkNodes);
	         
	       //Step1找出必须在与消费点直接相连的节点上设置服务器，设置了一条路线
	         FirstStep step1=new FirstStep();
	         step1.setNecessary(networkNodes, consumeNodes);
	       
	        //Step2找到消费点最近的消费点
	         SecondStep step2=new SecondStep();
	         List<Integer> severlocation= step2.findSeverLocaltion(matrix, networkNodes, consumeNodes);
	         
	         //Step3流量重新分配
	         ThirdStepCopy step3=new ThirdStepCopy();              
	        step3.adjust(severlocation, networkNodes, consumeNodes);
	        String[] result=step3.resultOutPut(step3.getSeverPathsResult(), networkNodes,consumeNodes);
	        return result;
		}
		
	}

}
